package com.dc.di.sf.dt;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.axiom.om.OMElement;

import com.dc.di.sf.utils.OMUtils;

public class Attachments implements DTObject<Attachments> {

	public static final String TAG_ATTACHMENTS = "Attachments";

	private List<Attachment> attachments = new ArrayList<Attachment>();

	public Attachments() {

	}

	/**
	 * @return the attachments
	 */
	public List<Attachment> getAttachments() {
		return attachments;
	}

	/**
	 * @param attachments
	 *            the attachments to set
	 */
	public void setAttachments(List<Attachment> attachments) {
		this.attachments = attachments;
	}

	public OMElement toXML() {
		OMElement data = OMUtils.createOMElement(TAG_ATTACHMENTS);
		for (Attachment attachment : attachments)
			data.addChild(attachment.toXML());
		return data;
	}

	public Attachments fromXML(OMElement xml) {
		if (xml != null)
			for (Iterator itr = OMUtils.getChildren(xml, Attachment.TAG_ATTACHMENT); itr.hasNext();)
				attachments.add(new Attachment().fromXML((OMElement) itr.next()));
		return this;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.dc.di.sf.dt.DTObject#toXMLString()
	 */
	public String toXMLString() {
		StringBuilder sb = new StringBuilder();
		sb.append(OMUtils.generateOMElementBeginString(TAG_ATTACHMENTS));
		for (Attachment attachment : attachments)
			sb.append(attachment.toXMLString());
		sb.append(OMUtils.generateOMElementEndString(TAG_ATTACHMENTS));
		return sb.toString();
	}


}
